
import { showModal, showToast } from "../../utils/asyncWx";
import { getOrderInfo,deleteOrderOne,createOrderOne,setOrderInfo} from "../../api/order.js";

Page({

    data: {
        tabs: [{
                id: 0,
                value: "待付款",
                isActive: true
            },
            {
                id: 1,
                value: "已付款",
                isActive: false
            },
            {
                id: 2,
                value: "退货中",
                isActive: false
            },
            {
                id: 3,
                value: "退款成功",
                isActive: false
            },
        ],
        order: [],
        Allorder:[]
    },
    handleTabsItemChange(e) {
        const { index } = e.detail;
        let { tabs } = this.data;
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
        this.setData({
            tabs
        })
        this.getOrderInfo()
    },


    onShow: function() {
        let userid= wx.getStorageSync('userid')
        if(!userid){
            wx.switchTab({
                url: '/pages/user/index',
            })
             showToast({ title: "请先登录" });
        }else{
            this.getOrderInfo();
            let pages = getCurrentPages();
            let currentPage = pages[pages.length - 1];
            console.log(currentPage.options);
            const { type } = currentPage.options;
            let index = type - 1;
            let { tabs } = this.data;
            tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
            this.setData({
                tabs
            })
        }
    },
   async handleReturnGoods(e){
        const userid = wx.getStorageSync('userid')
        console.log(e.currentTarget.dataset);
        const {id} = e.currentTarget.dataset
        console.log(userid);
        const order = await  deleteOrderOne(userid,id);
       this.getOrderInfo()
    },
   async getOrderInfo() {
       const userid = wx.getStorageSync('userid')
        let getorder = await getOrderInfo(userid)
        console.log(getorder);

      let Allorder=[[], [],[] ,[]]
      getorder.forEach(v=>{
          if(v.state==0){
            Allorder[0].push(v);
          }else if(v.state==1){
            Allorder[1].push(v);
          }else if(v.state==2){
            Allorder[2].push(v);
          }else {
            Allorder[3].push(v);
          }
      })
      let tabs = this.data.tabs
      tabs.forEach((v,index)=>{
          if(v.isActive){
              getorder = Allorder[index]
          }
      })
      if(getorder){
        this.setData({
            order:getorder,
            Allorder
        })
     }
    
    },
   async handleCancelOrder(e){
    const userid = wx.getStorageSync('userid')
    const {id} = e.currentTarget.dataset
    const order = await  deleteOrderOne(userid,id);
    this.getOrderInfo()
    },
   
    async handlePayGoods(e){
        const userid = wx.getStorageSync('userid')
        const {id} = e.currentTarget.dataset
        const order = await setOrderInfo(id,'1');
        this.getOrderInfo()
    },
   async handleReturnGoods(e){
    const userid = wx.getStorageSync('userid')
    const {id} = e.currentTarget.dataset
    const order = await setOrderInfo(id,'2');
    this.getOrderInfo()
   },
   async handleCancelReturn(e){
    const userid = wx.getStorageSync('userid')
    const {id} = e.currentTarget.dataset
    const order = await setOrderInfo(id,'1');
    this.getOrderInfo()
   }
})